<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Nginx 动态内容缓存失效策略完全指南</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #1e293b;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #0f172a;
            border-bottom: 1px solid #334155;
        }
        .code-block pre {
            margin: 0;
            padding: 60px 24px 24px;
            overflow-x: auto;
        }
        .code-block code {
            color: #e2e8f0;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
        }
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        .feature-card {
            background: rgba(255, 255, 255, 0.9);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .highlight-text {
            background: linear-gradient(120deg, #fbbf24 0%, #f59e0b 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 600;
        }
        .floating-icon {
            animation: float 3s ease-in-out infinite;
        }
        @keyframes float {
            0%, 100% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="text-center">
                <div class="floating-icon inline-block mb-6">
                    <i class="fas fa-server text-6xl opacity-90"></i>
                </div>
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Nginx 动态内容<br>
                    <span class="text-yellow-300">缓存失效策略</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                    掌握高性能网站的核心技术，让您的动态内容既快速又实时
                </p>
            </div>
        </div>
    </section>

    <!-- Introduction -->
    <section class="py-16 px-6">
        <div class="max-w-4xl mx-auto">
            <div class="feature-card rounded-2xl p-8 shadow-xl card-hover">
                <div class="flex items-start space-x-4">
                    <i class="fas fa-lightbulb text-3xl text-purple-600 mt-1"></i>
                    <div>
                        <h2 class="text-2xl font-bold mb-4 text-gray-800">核心概念</h2>
                        <p class="text-lg text-gray-700 leading-relaxed">
                            在 Nginx 中实现动态内容的缓存失效策略涉及到配置缓存机制以及制定缓存失效策略。动态内容通常是指会随着请求而改变的内容，比如数据库查询结果或实时生成的页面。通过合理的缓存策略，我们可以在保证内容实时性的同时，大幅提升网站性能。
                        </p>
                    </div>
                </div>
            </div>
        </div>
    </section>

    <!-- Visualization -->
    <section class="py-12 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="feature-card rounded-2xl p-8 shadow-xl">
                <h2 class="text-3xl font-bold text-center mb-8 text-gray-800">
                    <i class="fas fa-sitemap text-purple-600 mr-3"></i>缓存策略架构图
                </h2>
                <div class="mermaid">
                    graph TB
                        A[客户端请求] --> B{Nginx 缓存检查}
                        B -->|缓存命中| C[返回缓存内容]
                        B -->|缓存未命中| D[转发到后端]
                        D --> E[后端服务器]
                        E --> F[生成动态内容]
                        F --> G[返回响应]
                        G --> H[更新缓存]
                        H --> I[返回给客户端]
                        
                        J[缓存失效策略] --> K[基于时间]
                        J --> L[基于请求参数]
                        J --> M[基于响应头]
                        J --> N[手动清除]
                        
                        style A fill:#fbbf24
                        style B fill:#8b5cf6
                        style C fill:#10b981
                        style E fill:#3b82f6
                        style J fill:#ef4444
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <section class="py-16 px-6">
        <div class="max-w-6xl mx-auto">
            <!-- Section 1: 配置缓存机制 -->
            <div class="mb-16">
                <h2 class="section-title text-4xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-cog text-purple-600 mr-3"></i>1. 配置缓存机制
                </h2>
                
                <div class="grid md:grid-cols-2 gap-8 mb-8">
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-database text-blue-600 mr-2"></i>使用 proxy_cache
                        </h3>
                        <p class="text-gray-700 leading-relaxed">
                            Nginx 提供了 <code class="bg-gray-200 px-2 py-1 rounded text-purple-700 font-mono">proxy_cache</code> 模块用于缓存后端服务器的响应。通过合理配置，可以显著提升动态内容的响应速度。
                        </p>
                    </div>
                    
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-tachometer-alt text-green-600 mr-2"></i>性能优势
                        </h3>
                        <p class="text-gray-700 leading-relaxed">
                            缓存机制可以减少后端服务器的负载，降低响应延迟，提高并发处理能力，是构建高性能网站的关键技术。
                        </p>
                    </div>
                </div>

                <div class="code-block">
                    <div class="absolute top-3 left-4 flex space-x-2">
                        <div class="w-3 h-3 bg-red-500 rounded-full"></div>
                        <div class="w-3 h-3 bg-yellow-500 rounded-full"></div>
                        <div class="w-3 h-3 bg-green-500 rounded-full"></div>
                    </div>
                    <div class="absolute top-2 right-4 text-gray-400 text-sm">nginx.conf</div>
                    <pre><code>http {
    # 定义缓存区域
    proxy_cache_path /data/nginx/cache 
        levels=1:2 
        keys_zone=my_cache:10m 
        max_size=1g 
        inactive=60m 
        use_temp_path=off;

    server {
        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 1m;  # 正常响应缓存 1 分钟
            proxy_cache_valid 404 1m;  # 404 响应缓存 1 分钟
            proxy_cache_use_stale error timeout updating;  # 使用过期缓存
        }
    }
}</code></pre>
                </div>
            </div>

            <!-- Section 2: 缓存失效策略 -->
            <div class="mb-16">
                <h2 class="section-title text-4xl font-bold mb-8 text-gray-800">
                    <i class="fas fa-clock text-purple-600 mr-3"></i>2. 实现缓存失效策略
                </h2>

                <!-- Strategy Cards -->
                <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6 mb-12">
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover text-center">
                        <i class="fas fa-hourglass-half text-4xl text-blue-600 mb-4"></i>
                        <h4 class="text-xl font-semibold mb-2">基于时间</h4>
                        <p class="text-gray-600">设置缓存有效期</p>
                    </div>
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover text-center">
                        <i class="fas fa-filter text-4xl text-green-600 mb-4"></i>
                        <h4 class="text-xl font-semibold mb-2">基于参数</h4>
                        <p class="text-gray-600">根据请求参数缓存</p>
                    </div>
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover text-center">
                        <i class="fas fa-reply text-4xl text-purple-600 mb-4"></i>
                        <h4 class="text-xl font-semibold mb-2">基于响应</h4>
                        <p class="text-gray-600">遵循响应头指示</p>
                    </div>
                    <div class="feature-card rounded-xl p-6 shadow-lg card-hover text-center">
                        <i class="fas fa-trash-alt text-4xl text-red-600 mb-4"></i>
                        <h4 class="text-xl font-semibold mb-2">手动清除</h4>
                        <p class="text-gray-600">主动更新缓存</p>
                    </div>
                </div>

                <!-- Strategy Details -->
                <div class="space-y-8">
                    <!-- 2.1 基于时间的失效 -->
                    <div class="feature-card rounded-xl p-8 shadow-lg">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">
                            <i class="fas fa-hourglass-half text-blue-600 mr-2"></i>2.1 基于时间的失效
                        </h3>
                        <p class="text-gray-700 mb-4 leading-relaxed">
                            使用 <code class="bg-gray-200 px-2 py-1 rounded text-purple-700 font-mono">proxy_cache_valid</code> 配置缓存的时间长度。可以针对不同的状态码设置不同的缓存时间，实现精细化的缓存控制。
                        </p>
                        <div class="code-block">
                            <div class="absolute top-3 left-4 flex space-x-2">
                                <div class="w-3 h-3 bg-red-500 rounded-full"></div>
                                <div class="w-3 h-3 bg-yellow-500 rounded-full"></div>
                                <div class="w-3 h-3 bg-green-500 rounded-full"></div>
                            </div>
                            <pre><code>location / {
    proxy_pass http://backend;
    proxy_cache my_cache;
    proxy_cache_valid 200 1m;  # 200 响应缓存 1 分钟
    proxy_cache_valid 404 5m;  # 404 响应缓存 5 分钟
}</code></pre>
                        </div>
                    </div>

                    <!-- 2.2 基于请求参数的缓存 -->
                    <div class="feature-card rounded-xl p-8 shadow-lg">
                        <h3 class="text-2xl font-semibold mb-4 text-gray-800">